package br.com.formulavendas.businessusercase;
// default package

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.formulavendas.domainmodel.Funcionario;


/**
 * @see .Funcionario
 * @author MyEclipse Persistence Tools
 */
public class ManterFuncionario extends BaseBusinessUserCase 
	implements br.com.formulavendas.businessusercase.baseinterface.IBasePersistence<Funcionario>,
	br.com.formulavendas.businessusercase.baseinterface.IBaseFind<Funcionario> {

	public ManterFuncionario()  {
	}

	private static final Logger log = LoggerFactory
			.getLogger(ManterFuncionario.class);
	
	// property constants
	public static final String NOME = "nome";
	public static final String CPF = "cpf";
	public static final String RG = "rg";
	public static final String EMAIL = "email";

	@Override
	public void save(Funcionario transientInstance) {
		Transaction tx;
		log.debug("saving Funcionario instance");
		tx = this._session.beginTransaction();
		try {			
			this._session.save(transientInstance);
			tx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			tx.rollback();
			log.error("save failed", re);
			throw re;
		}
	}

	@Override
	public void delete(Funcionario persistentInstance) {
		log.debug("deleting Funcionario instance");
		try {
			this._session.delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	@Override
	public Funcionario findById(java.lang.Integer id) {
		log.debug("getting Funcionario instance with id: " + id);
		try {
			Funcionario instance = (Funcionario) this._session.get(
					"Funcionario", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Funcionario> findByExample(Funcionario instance) {
		log.debug("finding Funcionario instance by example");
		try {
			List results = this._session.createCriteria("Funcionario")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@Override
	public List<Funcionario> findByProperty(String propertyName, Object value) {
		log.debug("finding Funcionario instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Funcionario as model where model."
					+ propertyName + "= ?";
			Query queryObject = this._session.createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Funcionario> findByNome(Object nome) {
		return findByProperty(NOME, nome);
	}

	public List<Funcionario> findByCpf(Object cpf) {
		return findByProperty(CPF, cpf);
	}

	public List<Funcionario> findByRg(Object rg) {
		return findByProperty(RG, rg);
	}

	public List<Funcionario> findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	@Override
	public List<Funcionario> findAll() {
		log.debug("finding all Funcionario instances");
		try {
			String queryString = "from Funcionario";
			Query queryObject = this._session.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Funcionario merge(Funcionario detachedInstance) {
		log.debug("merging Funcionario instance");
		try {
			Funcionario result = (Funcionario) this._session
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Funcionario instance) {
		log.debug("attaching dirty Funcionario instance");
		try {
			this._session.saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Funcionario instance) {
		log.debug("attaching clean Funcionario instance");
		try {
			this._session.lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
}